import Food from './Food'
import ScorePanel from './ScorePanel'
import Snake from './Snake'
export default class GameController {
    food: Food
    scorepanel: ScorePanel
    snake: Snake
    direction: string = ''
    islive: boolean = true
    remake: HTMLElement
    r: HTMLElement
    b: HTMLElement
    l: HTMLElement
    t: HTMLElement
    maxscore: HTMLElement
    max: number
    chuan: HTMLElement
    upsudo: HTMLElement
    constructor() {
        this.food = new Food
        this.scorepanel = new ScorePanel(10, 10)
        this.snake = new Snake
        this.remake = document.getElementById('remake')
        this.chuan = document.getElementById('chuan')
        this.chuan.innerHTML = `${this.snake.ischuan ? '关闭' : '开启'}穿墙`
        this.chuan.addEventListener('click', this.changec.bind(this))
        this.r = document.getElementById('r')
        this.b = document.getElementById('b')
        this.l = document.getElementById('l')
        this.t = document.getElementById('t')
        this.maxscore = document.getElementById('maxscore')
        this.upsudo = document.getElementById('upsudu')
        this.max = Number(localStorage.getItem('max')) || 0
        this.maxscore.innerHTML = `最高分:<b>${this.max}</b>`
        this.init()
        this.run()
    }
    changec(){
        this.snake.ischuan = !this.snake.ischuan
        this.chuan.innerHTML = `${this.snake.ischuan ? '关闭' : '开启'}穿墙`
    }
    init() {
        document.addEventListener('keydown', this.keydownhandler.bind(this))
        this.r.addEventListener('click', this.ri.bind(this))
        this.t.addEventListener('click', this.ti.bind(this))
        this.b.addEventListener('click', this.bi.bind(this))
        this.l.addEventListener('click', this.li.bind(this))
        this.upsudo.addEventListener('click', this.up.bind(this))
    }
    keydownhandler(e: KeyboardEvent) {
        this.direction = e.key
    }
    ri() {
        this.direction = 'ArrowRight'
    }
    ti() {
        this.direction = 'ArrowUp'
    }
    bi() {
        this.direction = 'ArrowDown'
    }
    li() {
        this.direction = 'ArrowLeft'
    }
    up() { this.scorepanel.addlevel() }
    run() {
        let x = this.snake.X
        let y = this.snake.Y

        switch (this.direction) {
            case ('ArrowUp'):
                y -= 10
                break;
            case ('ArrowDown'):
                y += 10
                break;
            case ('ArrowRight'):
                x += 10
                break;
            case ('ArrowLeft'):
                x -= 10
                break;
        }
        this.checkEat(x, y)
        try {
            this.snake.X = x
            this.snake.Y = y
        } catch (error) {
            alert(error.message)
            this.islive = false
            if (this.max < this.scorepanel.score) {
                localStorage.setItem('max', this.scorepanel.score + '')
            }
            this.remake.style.display = 'block'
        }
        this.islive && setTimeout(this.run.bind(this), 300 - (this.scorepanel.level - 1) * 30)
    }
    checkEat(x: number, y: number) {
        if (x == this.food.X && y == this.food.Y) {
            this.food.changeP()
            this.scorepanel.addscore()
            this.snake.addBody()
        }
    }

}



